package com.hspedu.newData.dmsxl.哈希表;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-27 
 * @Version: 1.0
 */

public class LC205同构字符串 {

    // 哈希实现
    public boolean isIsomorphic(String s, String t) {
        Map<Character,Character> s2t = new HashMap<>();
        Map<Character,Character> t2s = new HashMap<>();

        int len = s.length();

        for (int i = 0; i < len; i++) {
            char x =s.charAt(i);
            char y = s.charAt(i);
            if ((s2t.containsKey(x) && s2t.get(x) != y) ||((t2s.containsKey(y)&&t2s.get(y) != x))){
                return false;
            }
            s2t.put(x,y);
            t2s.put(y,x);
        }
        return true;
    }

    // 将字符串转化为对应的每一个字符在字符串中第一次出现的下标
    // [g,o,o]转换成[0,1,1]
    // [f,a,a]转换成[0,1,1]
    // 通过比较相同位置的下标值是否相同

    public boolean isIsomorphic1(String s, String t) {
        for (int i = 0; i < s.length(); i++) {
            if (s.indexOf(s.charAt(i)) != t.indexOf(t.charAt(i))){
                return false;
            }
        }
        return true;
    }


    @Test
    public void test() {
        // 测试代码
        boolean isomorphic1 = isIsomorphic1("goo", "fac");
        System.out.println("------");
    }
}
